### Beginning ####
if(!require(starpolishr)){
  devtools::install_github("ChandlerLutz/starpolishr")
  library(starpolishr)
} else(library(starpolishr))
pacman::p_load(lme4, arm, haven, stargazer, dplyr)

lmm.data1 <- read_dta("./data_clean/psych_clean_long.dta") %>%
  dplyr::rename(id = Respondent_Serial, 
                narr_believe = response,
                narrative_num = question) %>% 
  rename(StoryTrue = true,
         CRT = zCRT_all,
         AOT = AOT_all,
         RussianOrientation = russianOrientation,
         ProRussiaPoliOrientation = zantiEuropeOrientation,
         Female = RSPSEX,
         Age = RSPAGE,
         Education = RSPEDUC,
         Income = income)

lmm.data2 <- read_dta("./data_clean/psych_clean_long2.dta") %>% 
  rename(StoryTrue = narr_true,
         CRT = zCRT_all,
         AOT = AOT_all,
         RussianOrientation = russianOrientation,
         # need to rename RussiaThreat to ProRussiaPoliOrientation to get for loop to run smoothly
         # make sure to fix name in regression table afterwards
         # RussiaThreat = zthreat_russian,
         ThreatRussian = zthreat_russian,
         Female = RSPSEX,
         Age = RSPAGE,
         Education = RSPEDUC,
         Income = INCOM,
         OblastStrata = strtcode)

both_rounds <- list(lmm.data1, lmm.data2)

###################
#Separate out the rounds and organize from least to most complicated
# random intercepts for statements and individuals with and without demographic controls

#There are 4 sets of models which are matrix of two main variables (analysis X motivated) (e.g, ) CRT and Orientation
# Each model has four specifications a-d
#-a varying intercepts on individual and narrative, varying slopes for story TRUE for each individual and CRT and Russian orientation + interation
#-b varying intercepts on individual and narrative, varying slopes for story TRUE for each individual and CRT and Russian orientation - NO interation
#-c varying intercepts on individual and narrative 
#-c varying intercepts on individual and narrative and demographic controls

# +++++++++++++++++++
# Study 1 Models ####
# +++++++++++++++++++

set.seed(9)
model1a <- lme4::lmer(narr_believe ~ StoryTrue*CRT*RussianOrientation + 
                        (1 + CRT*RussianOrientation | narrative_num) + 
                        (1 + StoryTrue  | id),
                      data = lmm.data1)

set.seed(92)
model1b <- lme4::lmer(narr_believe ~ StoryTrue*CRT*RussianOrientation +
                  (1 + CRT + RussianOrientation | narrative_num) + 
                  (1 + StoryTrue  | id),
                  data = lmm.data1)

set.seed(2)
model1c <- lme4::lmer(narr_believe ~ StoryTrue*CRT*RussianOrientation + 
                  (1  | narrative_num) + 
                  (1  | id), 
                  data = lmm.data1)
  
set.seed(920)
model1d <- lme4::lmer(narr_believe ~ StoryTrue*CRT*RussianOrientation + 
                  Age + Female +as.factor(Education) + Income +
                  (1  | narrative_num) + 
                  (1  | id), 
                data = lmm.data1)
  
set.seed(91)
model2a <- lme4::lmer(narr_believe ~ StoryTrue*AOT*RussianOrientation + 
                  (1 + AOT*RussianOrientation | narrative_num) + 
                  (1 + StoryTrue  | id), 
                 data = lmm.data1)

set.seed(73)
model2b <- lme4::lmer(narr_believe ~ StoryTrue*AOT*RussianOrientation + 
                  (1 + AOT + RussianOrientation | narrative_num) +
                  (1 + StoryTrue  | id), 
                  data = lmm.data1)

set.seed(922)
model2c <- lme4::lmer(narr_believe ~ StoryTrue*AOT*RussianOrientation + 
                  (1  | narrative_num) + 
                  (1  | id), 
                  data = lmm.data1)

set.seed(81)
model2d <- lme4::lmer(narr_believe ~ StoryTrue*AOT*RussianOrientation + 
                    Age + Female +as.factor(Education) + Income +
                    (1  | narrative_num) + 
                    (1  | id), 
                  data = lmm.data1)
  
set.seed(8712)
model3a <- lme4::lmer(narr_believe ~ StoryTrue*CRT*ProRussiaPoliOrientation + 
                   (1 + CRT*ProRussiaPoliOrientation | narrative_num) + 
                   (1 + StoryTrue  | id), 
                 data = lmm.data1)

set.seed(7)
model3b <- lme4::lmer(narr_believe ~ StoryTrue*CRT*ProRussiaPoliOrientation + 
                    (1 + CRT + ProRussiaPoliOrientation | narrative_num) + 
                    (1 + StoryTrue  | id), 
                  data = lmm.data1)

set.seed(12)
model3c <- lme4::lmer(narr_believe ~ StoryTrue*CRT*ProRussiaPoliOrientation + 
                    (1  | narrative_num) + 
                    (1  | id), 
                  data = lmm.data1)

set.seed(32)
model3d <- lme4::lmer(narr_believe ~ StoryTrue*CRT*ProRussiaPoliOrientation +
                    Age + Female +as.factor(Education) + Income + 
                    (1  | narrative_num) +
                    (1  | id), 
                  data = lmm.data1)
  
set.seed(1234)
model4a <- lme4::lmer(narr_believe ~ StoryTrue*AOT*ProRussiaPoliOrientation + 
                  (1 + AOT*ProRussiaPoliOrientation | narrative_num) + 
                  (1 + StoryTrue  | id), 
                 data = lmm.data1)

set.seed(84)
model4b <- lme4::lmer(narr_believe ~ StoryTrue*AOT*ProRussiaPoliOrientation + 
                  (1 + AOT + ProRussiaPoliOrientation | narrative_num) +
                  (1 + StoryTrue  | id), 
                  data = lmm.data1)

set.seed(83)
model4c <- lme4::lmer(narr_believe ~ StoryTrue*AOT*ProRussiaPoliOrientation +
                  (1  | narrative_num) +
                  (1  | id), 
                  data = lmm.data1)

set.seed(64)
model4d <- lme4::lmer(narr_believe ~ StoryTrue*AOT*ProRussiaPoliOrientation + 
                  Age + Female +as.factor(Education) + Income +
                  (1  | narrative_num) + 
                  (1  | id), 
                  data = lmm.data1)
  
all_models <- list(model1a, model1b, model1c, model1d, model2a, model2b, model2c, model2d, model3a, model3b, model3c, model3d, model4a, model4b, model4c, model4d)
save(all_models, file = "./data_clean/round_1_multilevel.Rdata")
rm(all_models)

# +++++++++++++++++++
# Study 2 Models ####
# +++++++++++++++++++
set.seed(44)
model1a <- lme4::lmer(narr_believe ~ StoryTrue*CRT*RussianOrientation +  as_factor(OblastStrata) +
                        (1 + CRT*RussianOrientation | narrative_num) + 
                        (1 + StoryTrue  | id),
                      data = lmm.data2)

set.seed(60)
model1b <- lme4::lmer(narr_believe ~ StoryTrue*CRT*RussianOrientation + as_factor(OblastStrata) +
                        (1 + CRT + RussianOrientation | narrative_num) + 
                        (1 + StoryTrue  | id),
                      data = lmm.data2)

set.seed(422)
model1c <- lme4::lmer(narr_believe ~ StoryTrue*CRT*RussianOrientation + as_factor(OblastStrata) + 
                        (1  | narrative_num) + 
                        (1  | id), 
                      data = lmm.data2)

set.seed(124)
model1d <- lme4::lmer(narr_believe ~ StoryTrue*CRT*RussianOrientation + 
                        Age + Female +as.factor(Education) + Income + as_factor(OblastStrata) +
                        (1  | narrative_num) + 
                        (1  | id), 
                      data = lmm.data2)

set.seed(55)
model2a <- lme4::lmer(narr_believe ~ StoryTrue*AOT*RussianOrientation +  as_factor(OblastStrata) +
                        (1 + AOT*RussianOrientation | narrative_num) + 
                        (1 + StoryTrue  | id), 
                      data = lmm.data2)

set.seed(111)
model2b <- lme4::lmer(narr_believe ~ StoryTrue*AOT*RussianOrientation +  as_factor(OblastStrata) +
                        (1 + AOT + RussianOrientation | narrative_num) +
                        (1 + StoryTrue  | id), 
                      data = lmm.data2)

set.seed(25)
model2c <- lme4::lmer(narr_believe ~ StoryTrue*AOT*RussianOrientation +  as_factor(OblastStrata) +
                        (1  | narrative_num) + 
                        (1  | id), 
                      data = lmm.data2)

set.seed(37)
model2d <- lme4::lmer(narr_believe ~ StoryTrue*AOT*RussianOrientation + 
                        Age + Female +as.factor(Education) + Income + as_factor(OblastStrata) +
                        (1  | narrative_num) + 
                        (1  | id), 
                      data = lmm.data2)

set.seed(72)
model3a <- lme4::lmer(narr_believe ~ StoryTrue*CRT*ThreatRussian +  as_factor(OblastStrata) +
                        (1 + CRT*ThreatRussian | narrative_num) + 
                        (1 + StoryTrue  | id), 
                      data = lmm.data2)

set.seed(613)
model3b <- lme4::lmer(narr_believe ~ StoryTrue*CRT*ThreatRussian +  as_factor(OblastStrata) +
                        (1 + CRT + ThreatRussian | narrative_num) + 
                        (1 + StoryTrue  | id), 
                      data = lmm.data2)

set.seed(925)
model3c <- lme4::lmer(narr_believe ~ StoryTrue*CRT*ThreatRussian +  as_factor(OblastStrata) +
                        (1  | narrative_num) + 
                        (1  | id), 
                      data = lmm.data2)

set.seed(545)
model3d <- lme4::lmer(narr_believe ~ StoryTrue*CRT*ThreatRussian +  as_factor(OblastStrata) +
                        Age + Female +as.factor(Education) + Income + as_factor(OblastStrata) + 
                        (1  | narrative_num) +
                        (1  | id), 
                      data = lmm.data2)

set.seed(7764)
model4a <- lme4::lmer(narr_believe ~ StoryTrue*AOT*ThreatRussian +  as_factor(OblastStrata) +
                        (1 + AOT*ThreatRussian | narrative_num) + 
                        (1 + StoryTrue  | id), 
                      data = lmm.data2)

set.seed(7232)
model4b <- lme4::lmer(narr_believe ~ StoryTrue*AOT*ThreatRussian +  as_factor(OblastStrata) +
                        (1 + AOT + ThreatRussian | narrative_num) +
                        (1 + StoryTrue  | id), 
                      data = lmm.data2)

set.seed(25)
model4c <- lme4::lmer(narr_believe ~ StoryTrue*AOT*ThreatRussian + as_factor(OblastStrata) +
                        (1  | narrative_num) +
                        (1  | id), 
                      data = lmm.data2)

set.seed(5256)
model4d <- lme4::lmer(narr_believe ~ StoryTrue*AOT*ThreatRussian + 
                        Age + Female +as.factor(Education) + Income + as_factor(OblastStrata) +
                        (1  | narrative_num) + 
                        (1  | id), 
                      data = lmm.data2)

all_models <- list(model1a, model1b, model1c, model1d, model2a, model2b, model2c, model2d, model3a, model3b, model3c, model3d, model4a, model4b, model4c, model4d)
save(all_models, file = "./data_clean/round_2_multilevel.Rdata")
